package com.firesrc.web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.db.DbThreadSession;
import jodd.db.oom.DbOomQuery;

import com.firesrc.bean.CountBean;

/**
 * Servlet implementation class ViewAllKeyword
 */
public class ViewAllTopic extends SuperServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewAllTopic() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {
		List<CountBean> list = getAllCountBeanList();
		request.setAttribute("list", list);
		request.setAttribute("sendMap", getAllUnSendCountBeanMap());
		request.getRequestDispatcher("allTopicMailCount.jsp").forward(request, response);
	}

	private List<CountBean> getAllCountBeanList() {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		DbOomQuery q = new DbOomQuery("select topic as name ,count(mail) as count from record  group by topic");
		ResultSet execute = q.execute();
		//		StringBuffer buffer = new StringBuffer();
		List<CountBean> list = new ArrayList<CountBean>();
		CountBean bean = null;
		try {
			while (execute.next()) {
				bean = new CountBean();
				bean.setName(execute.getString(1));
				bean.setCount(execute.getInt(2));
				list.add(bean);
				//				buffer.append("<a href='/QueryMailByTopic?topic=" + execute.getString(1) + "' a>"
				//						+ execute.getString(1) + "(" + execute.getInt(2) + ") </a>  &nbsp; &nbsp; ");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		dbThreadSession.closeSession();
		return list;
	}

	private Map<String, Integer> getAllUnSendCountBeanMap() {
		DbThreadSession dbThreadSession = new DbThreadSession(cp);
		DbOomQuery q = new DbOomQuery(
				"select topic as name ,count(mail) as count from record  where mailstate='1' group by topic");
		ResultSet execute = q.execute();
		//		StringBuffer buffer = new StringBuffer();
		Map<String, Integer> map = new HashMap<String, Integer>();
		try {
			while (execute.next()) {
				map.put(execute.getString(1), execute.getInt(2));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		dbThreadSession.closeSession();
		return map;
	}
}
